<script setup>
import mainAside from './component/main-aside.vue'
import { computed, ref, nextTick } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import cache from '@/util/cache.js'
const store = useStore()
const router = useRouter()
const handOutClick = () => {
  cache.deleteCache('jxcms-token')
  cache.deleteCache('jxcms-user')
  router.push('/login')
}
const isCollapse = ref(false)
const handCollapseClick = () => {
  isCollapse.value = !isCollapse.value
}
//请求数据
store.dispatch('requestNav')
const navList = computed(() => {
  return store.state.nav.navList
})
//请求角色信息
store.dispatch('requestRoles')
const rolesList = computed(() => {
  return store.state.roles.rolesList
})
const ruleFormRef = ref(null)
const dialogFormVisible = ref(false)
const isAdd = ref('')
const formLabelAlign = ref({
  rName: '',
  rPower: ''
})
const handAddClick = () => {
  isAdd.value = 'add'
  formLabelAlign.value = {
    rName: '',
    rPower: ''
  }
  nextTick(() => {
    elTreeRef.value.setCheckedKeys([], false)
  })
  dialogFormVisible.value = true
}
const handAddReqClick = async () => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (isAdd.value === 'add') {
        formLabelAlign.value.rPower = elTreeRef.value.getCheckedKeys().join(',')
        store.dispatch('addRequestRoles', formLabelAlign.value)
        isAdd.value = ''
      } else {
        formLabelAlign.value.rPower = elTreeRef.value.getCheckedKeys().join(',')
        //编辑时提交
        store.dispatch('EditRequestRoles', formLabelAlign.value)
      }
      formLabelAlign.value = {
        rName: '',
        rPower: ''
      }
      dialogFormVisible.value = false
    } else {
      console.log('error submit!', fields)
    }
  })
}
const handDeteleClick = (item) => {
  store.dispatch('deteleRequestRoles', item.rId)
}
const rules = ref({
  rName: [{ required: true, message: '必须填写', trigger: 'blur' }],
  rPower: [{ required: true, message: '必须填写', trigger: 'blur' }]
})
//获取选择的节点
const elTreeRef = ref(null)
const handEditClick = (item) => {
  dialogFormVisible.value = true
  nextTick(() => {
    //设置本身拥有的权限
    elTreeRef.value.setCheckedKeys(item.rPower.split(','), false)
  })
  formLabelAlign.value = item
}
const handCancelClick = () => {
  dialogFormVisible.value = false
  isAdd.value = ''
}
</script>

<template>
  <div class="roles">
    <div class="common-layout">
      <el-container>
        <el-aside :width="isCollapse ? '60px' : '216px'">
          <mainAside :isCollapse="isCollapse"></mainAside>
        </el-aside>
        <el-container>
          <el-header>
            <div class="header">
              <el-icon size="30" @click="handCollapseClick"><Fold /></el-icon>
              <div class="user">
                <img
                  src="https://s1.boluo.link/imgs/2022/12/02/3ac0a824e1fd00cb.jpg"
                />
                <el-dropdown>
                  <span class="el-dropdown-link">
                    <span>{{ store.state.user.userInfo.uName }}</span>
                    <el-icon class="el-icon--right">
                      <arrow-down />
                    </el-icon>
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item>修改用户信息</el-dropdown-item>
                      <el-dropdown-item divided @click="handOutClick"
                        >退出登录</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </div>
          </el-header>
          <el-main>
            <div class="main">
              <div class="addBtn">
                <el-button type="primary" @click="handAddClick"
                  >添加角色</el-button
                >
                <!--                添加弹出框-->
                <el-dialog
                  v-model="dialogFormVisible"
                  :close-on-click-modal="false"
                  :title="isAdd == 'add' ? '添加角色' : '编辑角色'"
                  :show-close="false"
                  width="30%"
                  center
                >
                  <el-form
                    ref="ruleFormRef"
                    :rules="rules"
                    label-width="100px"
                    :model="formLabelAlign"
                    hide-required-asterisk
                  >
                    <el-form-item label="角色名称" prop="rName">
                      <el-input v-model="formLabelAlign.rName" />
                    </el-form-item>
                    <el-form-item label="角色权限">
                      <el-tree
                        ref="elTreeRef"
                        :data="navList"
                        show-checkbox
                        node-key="nId"
                        :props="{ label: 'nName' }"
                      />
                    </el-form-item>
                  </el-form>

                  <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="handCancelClick">取消</el-button>
                      <el-button type="primary" @click="handAddReqClick">
                        添加
                      </el-button>
                    </span>
                  </template>
                </el-dialog>
              </div>
              <div class="tableBox">
                <el-table :data="rolesList" style="width: 100%">
                  <el-table-column
                    label="序号"
                    type="index"
                    align="center"
                    width="120"
                  />
                  <el-table-column
                    prop="rName"
                    label="角色名称"
                    min-width="120"
                    align="center"
                  />
                  <el-table-column
                    prop="rPower"
                    label="角色权限"
                    min-width="120"
                    align="center"
                  />
                  <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                      <el-button
                        type="primary"
                        @click="handEditClick(scope.row)"
                        >编辑</el-button
                      >
                      <el-button
                        type="danger"
                        @click="handDeteleClick(scope.row)"
                        >删除</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </el-main>
        </el-container>
      </el-container>
    </div>
  </div>
</template>

<style lang="less" scoped>
.el-aside {
  transition: width 0.5s;
  -webkit-transition: width 0.5s;
  -moz-transition: width 0.5s;
  -webkit-transition: width 0.5s;
  -o-transition: width 0.5s;
}
:deep(.el-aside) {
  margin-right: 6px;
  box-shadow: 6px 0 15px rgba(119, 119, 119, 0.1);
}
.el-header {
  height: 52px;
  background: #fff;
}
.el-main {
  background: #f3f5f8;
}
.el-icon {
  cursor: pointer;
}
.header {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .user {
    display: flex;
    align-items: center;
    margin-right: 30px;
    img {
      margin-right: 10px;
      width: 34px;
      height: 34px;
      border-radius: 10px;
    }
  }
}
</style>
